package com.winit.openapi.ums.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.winit.ims.spi.service.CompanyService;
import com.winit.openapi.constants.ApiConstant;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.CustomerModifyCommand;
import com.winit.ums.spi.customer.command.ModifyMeasurementCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.BpartnerService;
import com.winit.erp.spi.bpartner.command.BpartnerCommand;
import com.winit.erp.spi.bpartner.command.BpartnerMeasurementSystemCommand;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.mms.spi.common.Constant;
import com.winit.openapi.constants.EnumConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.sms.spi.wallet.creditaccount.CreditAccountService;
import com.winit.sms.spi.wallet.selleradvance.AdvanceSevice;
import com.winit.sms.spi.wallet.selleradvance.OrgConfigService;
import com.winit.sms.spi.wallet.selleradvance.command.OrgConfigCommand;
import com.winit.sms.spi.wallet.selleradvance.command.QueryAdVanceCommand;
import com.winit.sms.spi.wallet.selleradvance.vo.AdvanceVo;
import com.winit.sms.spi.wallet.selleradvance.vo.OrgConfigVo;

@Controller
@RequestMapping(value = "/preferences")
public class Preferences extends BaseController {

    @Resource
    private BpartnerService      bpartnerService;
    private String               PARAMETER_MEASUREMENTSYSTEM = "measurementSystem";
    private String               CUSTOMER_CODE="code";
    @Resource
    private CreditAccountService creditAccountService;

    @Resource
    private AdvanceSevice        advanceSevice;
    
    @Resource
    private OrgConfigService     orgConfigService;

    @Resource
    private UmsCustomerService    umsCustomerService;

    @RequestMapping(value = "/updatePreferences")
    public String updateMeasurementSystem(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        BpartnerMeasurementSystemCommand command = new BpartnerMeasurementSystemCommand();
        ModifyMeasurementCommand modifyMeasurementCommand = new ModifyMeasurementCommand();
        if (json.containsKey(PARAMETER_MEASUREMENTSYSTEM)) {
            String measurementSystem = json.getString(PARAMETER_MEASUREMENTSYSTEM).trim();
            try {
                EnumConstant.MeasurementSystem.valueOf(measurementSystem);
            } catch (Exception e) {
                throw new ApiException(ErrorCode.PARAMETER_VERIFICATION_FAILED, PARAMETER_MEASUREMENTSYSTEM);
            }
            command.setMeasurementSystem(measurementSystem);
            modifyMeasurementCommand.setMeasurementSystem(measurementSystem);
        } else {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, PARAMETER_MEASUREMENTSYSTEM);
        }
        CommandContext ctx = CommandContext.getContext();
        Long cBpartnerId = Long.valueOf(ctx.getUserId());
        command.setcBpartnerId(cBpartnerId);
        command.setCtx(ctx);
        bpartnerService.updateMeasurementSystem(command);
        modifyMeasurementCommand.setCtx(ctx);
        Bpartner bpartner = command.getCtx().get(ApiConstant.API_BPARTNER);
        modifyMeasurementCommand.setCode(bpartner.getValue());
        umsCustomerService.updateMeasurement(modifyMeasurementCommand);
        return SUCCESS;
    }

    @RequestMapping(value = "/getPreferences")
    public String getMeasurementSystem(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        BpartnerCommand command = new BpartnerCommand();
        CommandContext ctx = CommandContext.getContext();
        Long cBpartnerId = Long.valueOf(ctx.getUserId());
        command.setcBpartnerId(cBpartnerId);
        Bpartner bpartner = bpartnerService.queryBpartnerById(command);
        if (null != bpartner) {
            Map<String, Object> data = new HashMap<String, Object>();
            String measurementSystem = bpartner.getMeasurementSystem();

            QueryAdVanceCommand qcommand = new QueryAdVanceCommand();
            qcommand.setCustomerCode(bpartner.getValue());
            List<AdvanceVo> aList = advanceSevice.getAdvanceVoListByLogin(qcommand);

            OrgConfigCommand oCommand = new OrgConfigCommand();
            oCommand.setOrgId(CommandContext.getContext().getOrgId()+"");
            oCommand.setCtx(CommandContext.getContext());
            List<OrgConfigVo> oList = orgConfigService.queryOrgConfigEntityList(oCommand);
            boolean advanceFlag = true;
            if (oList != null && oList.size() > 0) {
                OrgConfigVo vo = oList.get(0);
                if (Constant.NOT_ACTIVE.equals(vo.getAdvanceFlag())) {
                    advanceFlag = false;
                }
            }
            data.put("advanceFlag", advanceFlag);//是否显示代付协议
            data.put("advanceList", aList);//代付拦截列表数据
            data.put("measurementSystem", measurementSystem);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

}
